package com.poi.entity;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.poi.util.LeadHandlerUtil;
import com.poi.util.PingYinUtil;
import com.poi.util.TemplateUtil;

public class ExportRunnable implements Runnable{
	
	private static final Logger logger = LogManager.getLogger(ExportRunnable.class);
	
	private List<TableEntity> tableEntity;
	private String outPath;
	private String outFullPath;
	private String tableName;
	private String tableComment;
	private String tablePrefix;
	private String primaryKey;
	private String primaryKeyUp;
	
	
	public ExportRunnable(){}
	
	public ExportRunnable(File file,String outPath,String tablePrefixCn){
		this.tableEntity = LeadHandlerUtil.getNameByFile(file);
		String fileName = file.getName().split("\\.")[0];
		String fileNamepy = PingYinUtil.getFirstSpell(fileName);
		this.outPath = outPath;
		this.tablePrefix = PingYinUtil.getFirstSpell(tablePrefixCn);
		this.tableComment=tablePrefixCn+"_"+fileName;
		this.tableName = this.tablePrefix+"_"+fileNamepy;
		this.outFullPath = this.outPath+this.tablePrefix+"_"+fileNamepy+".sql";
		this.primaryKey = fileNamepy+"_id";
		this.primaryKeyUp = primaryKey.toUpperCase();
	}

	
	
	
	public List<TableEntity> getTableEntity() {
		return tableEntity;
	}

	public void setTableEntity(List<TableEntity> tableEntity) {
		this.tableEntity = tableEntity;
	}

	public String getOutPath() {
		return outPath;
	}

	public void setOutPath(String outPath) {
		this.outPath = outPath;
	}

	public String getOutFullPath() {
		return outFullPath;
	}

	public void setOutFullPath(String outFullPath) {
		this.outFullPath = outFullPath;
	}

	
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getTableComment() {
		return tableComment;
	}

	public void setTableComment(String tableComment) {
		this.tableComment = tableComment;
	}

	public String getTablePrefix() {
		return tablePrefix;
	}

	public void setTablePrefix(String tablePrefix) {
		this.tablePrefix = tablePrefix;
	}

	public String getPrimaryKey() {
		return primaryKey;
	}

	public void setPrimaryKey(String primaryKey) {
		this.primaryKey = primaryKey;
	}

	public String getPrimaryKeyUp() {
		return primaryKeyUp;
	}

	public void setPrimaryKeyUp(String primaryKeyUp) {
		this.primaryKeyUp = primaryKeyUp;
	}
	

	@Override
	public void run() {
		// TODO Auto-generated method stub
		Path p2 = Paths.get(this.getOutFullPath());
		  try {
			Files.write(p2, TemplateUtil.createTemplate(this).getBytes(StandardCharsets.UTF_8));
			logger.info("-----------生成 "+this.tableName+".sql 文件完毕------------");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
